<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 10:02
 */

namespace App\Controller;
use DB;
use View;

class BaseController
{
    protected function redirect($url)
    {
        header("Location: {$url}");
    }

    protected function isLogin()
    {
        session_start();
        if (isset($_SESSION['user_id'])) {
            return true;
        }
        if (isset($_COOKIE['rem_token'])) {
            $user = DB::table('user')->where('remember_token', '=', $_COOKIE['rem_token'])->first();

            if ($user) {
                $_SESSION['user_id'] = $user['id'];
                $_SESSION['user_name'] = $user['user_name'];
                return true;
            }
        }
        return false;
    }

    /**
     * @param $cur_page 当前页
     * @param $total_item 共多少条记录
     * @param int $show_len 分页按钮左右各显示的个数
     * @param int $page_size 一个页面显示的记录条数
     */
    protected function getPager($cur_page, $total_item, $show_len = 3, $page_size = 12)
    {
        $cur_page = intval($cur_page);
        $total_item = intval($total_item);

        //没有数据，无需分页
        if ($total_item <= 0) {
            return false;
        }
        $pn['total'] = ceil($total_item / $page_size);

        if ($cur_page <= 0) {
            $cur_page = 1;
        }
        if ($cur_page > $pn['total']) {
            $cur_page = $pn['total'];
        }
        $pn['cur'] = $cur_page;

        $left = 1;
        if ($cur_page - $show_len > 0) {
            $left = $cur_page - $show_len;
        }
        $right = $pn['total'];
        if ($cur_page + $show_len <= $pn['total']) {
            $right = $cur_page + $show_len;
        }
        for ($i = $left; $i <= $right; $i++) {
            $pn['list'][] = $i;
        }

        return $pn;

    }

    public function msg($msg,$gourl,$goname,$sec =3){
        View::set('msg',$msg);
        View::set('gourl',$gourl);
        View::set('goname',$goname);
        View::set('sec',$sec);
        View::show('admin/msg');

    }

}